package com.billing.mvc.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.billing.persistance.entities.BillingEntry;
import com.billing.service.IBillingService;

@Controller
public class NavigationController {

	@Autowired
	private IBillingService billingService;	

	@ModelAttribute("billingEntryCriteria")
    public BillingEntry createNewBillingEntryCriteria() {
        return new BillingEntry();
    }
	
	private Logger logger = LoggerFactory.getLogger(NavigationController.class);
	
	private PagedListHolder<BillingEntry> entries;
	
	@ModelAttribute("billingList")
	public PagedListHolder<BillingEntry> getMessages(){
		
		if (entries == null){
			entries = new PagedListHolder<BillingEntry>(billingService.getBillingEntries());
		}
		
		return entries;
	}
	
	@RequestMapping("/Navigate")
	public String navigateToPage(@RequestParam("newPage")String newPage, ModelMap model) {
		
		if (newPage == null || newPage.equals(""))	{
			return "redirect:/";
		}
			
		if (model.containsKey("billingList")){
			
			PagedListHolder<BillingEntry> messageList =  (PagedListHolder<BillingEntry>) model.get("billingList");
			
			if (messageList.getSource() == null || messageList.getSource().size() == 0){
				return "redirect:/";
			}
			
			if (newPage.equalsIgnoreCase("NEXT")){
				if (!messageList.isLastPage()){
					messageList.setPage(messageList.getPage() + 1);
				}
			}
			else if (newPage.equalsIgnoreCase("PREVIOUS")){
				if (!messageList.isFirstPage()){
					messageList.setPage(messageList.getPage() - 1);
				}
			}
			model.addAttribute("billingList", messageList);
			model.addAttribute("urlPrefix", "billing");
			return "list";
		}
		else {
			return "redirect:/";
		}
	}
	
	@RequestMapping("/SetPageSize")
	public String setPageSize(@RequestParam("pageSize")String pageSize, ModelMap model){
		
		if (pageSize == null || pageSize.equals(""))	{
			return "redirect:/";
		}
		
		if (model.containsKey("billingList")){
			
			PagedListHolder<BillingEntry> messageList =  (PagedListHolder<BillingEntry>) model.get("billingList");
			
			
			try {
				messageList.setPageSize(Integer.valueOf(pageSize));
			}
			catch (NumberFormatException ex){
				logger.debug("Invalid page size given: " + pageSize );
			}
			
			model.addAttribute("billingList", messageList);
			return "list";
		}
		else {
			return "redirect:/";
		}
		
	}
	
	
	@RequestMapping("/SetPageOrder")
	public String setPageOrder(@RequestParam("pageOrder")String pageOrder, @RequestParam("ascending")boolean ascending, ModelMap model){
		
		if (pageOrder == null || pageOrder.equals(""))	{
			return "redirect:/Get/All";
		}
		
		logger.debug("Aplying sort to message list: Property: [" + pageOrder + "] - Ascending: [" + ascending +	"]");
		
		if (model.containsKey("billingList")){
			
			PagedListHolder<BillingEntry> messageList =  (PagedListHolder<BillingEntry>) model.get("billingList");
			
			if (messageList.getSort() != null){
				
				if (messageList.getSort().getProperty().equalsIgnoreCase(pageOrder)){
					((MutableSortDefinition)messageList.getSort()).setAscending(!messageList.getSort().isAscending());
				}
				else {
					MutableSortDefinition sort = new MutableSortDefinition();
					sort.setProperty(pageOrder);
					sort.setAscending(true);
					messageList.setSort(sort);
				}
				
			}
			
			messageList.resort();
			
			
			model.addAttribute("billingList", messageList);
			return "list";
		}
		else {
			return "redirect:/";
		}
		
	}
	
	

}
